package com.luoye.lj.wstool.web;

import com.luoye.lj.wstool.generator.ICodeGenerator;
import com.luoye.lj.wstool.web.entity.WebtEntity;
import com.luoye.lj.wstool.web.entity.WebtField;

/*
 * Created with IntelliJ IDEA.
 * User: Dave Liu
 * Date: 2016/4/26
 * Time: 15:47
 * To change this template use File | Settings | File Templates.
 */
public class ConstructorCodeGenerator implements ICodeGenerator {
    private WebtEntity webtEntity;
    private String suffix;

    public ConstructorCodeGenerator(WebtEntity webtEntity) {
        this.webtEntity = webtEntity;
    }

    public ConstructorCodeGenerator(WebtEntity webtEntity, String suffix) {
        this.webtEntity = webtEntity;
        this.suffix = suffix;
    }

    @Override
    public String codeGenerate() {
        String code="";
        String fieldsCode="";
        String getterSetterCode="";
        if(this.webtEntity!=null){
            if(this.webtEntity.getFieldList()!=null){
                for(WebtField webtField:this.webtEntity.getFieldList()){
                    fieldsCode+=""+webtField.getFieldType()+" "+webtField.getFieldName()+",";
                    getterSetterCode+="        this."+webtField.getFieldName()+" = "+webtField.getFieldName()+";\n";
                }
            }
        }
        if(!"".equals(fieldsCode)){
            fieldsCode=fieldsCode.substring(0,fieldsCode.length()-1);
            if(suffix!=null){
                code += "public " + webtEntity.getEntityName() + suffix + "(" + fieldsCode + ") {\n";
            }else {
                code += "public " + webtEntity.getEntityName() + "(" + fieldsCode + ") {\n";
            }
            code+=getterSetterCode;
            code+="    }\n";
        }
        return code;
    }
}
